package hxg.thread.safethread;

import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author HeXiangGuang
 * @create 2021-06-24 22:21
 */
public class Demo2 {
    public static void main(String[] args) {
        //创建集合
        CopyOnWriteArrayList<String> copyList = new CopyOnWriteArrayList<>();
        //创建线程池
        ExecutorService es = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 5; i++) {

            es.submit(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < 10; j++) {
                        copyList.add(Thread.currentThread().getName() + "===" + new Random().nextInt(100));
                    }
                }
            });
        }
            es.shutdown();
            while (!es.isTerminated()){

            }
            System.out.println("元素的个数"+copyList.size());
            for (String string : copyList) {
                System.out.println(string);
            }
        }
}
